package com.mdinfo.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.mdinfo.base.common.bean.CommonResult;
import com.mdinfo.base.common.bean.DataGrid;
import com.mdinfo.base.common.bean.MemberInfo;
import com.mdinfo.base.common.bean.Ref;
import com.mdinfo.base.utils.StringUtil;
import com.mdinfo.base.utils.SystemUtil;
import com.mdinfo.base.utils.TimeUtil;
import com.mdinfo.system.bean.SysMenu;
import com.mdinfo.system.mapper.SysMenuMapper;
import com.mdinfo.system.param.MenuParam;
import com.mdinfo.system.service.IMenuService;
import com.mdinfo.system.vo.MenuVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/11/22.
 */
@Service
@Transactional
public class MenuServiceImpl implements IMenuService {

    @Resource
    private SysMenuMapper menuMapper;

    /**
     * 查询所有权限
     */
    public CommonResult<List<MenuVo.ListAllVo>> listAll() {

        List<SysMenu> sysMenus = menuMapper.listAll();
        List<MenuVo.ListAllVo> vos = sysMenus.stream().map(menu -> {
            MenuVo.ListAllVo vo = new MenuVo.ListAllVo();
            vo.setMenuId(menu.getId());
            vo.setCreateTime(TimeUtil.formater(menu.getCreateTime()));
            vo.setIcon(menu.getIcon());
            vo.setName(menu.getName());
            vo.setOrderNum(menu.getOrderNum());
            vo.setPid(menu.getPid());
            SysMenu pmenu = menuMapper.selectById(menu.getPid());
            if (pmenu != null) {
                vo.setPname(pmenu.getName());
            }
            vo.setTag(menu.getTag());
            vo.setType(menu.getType());
            vo.setUrl(menu.getUrl());
            vo.setUsable(menu.getUsable());
            return vo;
        }).collect(Collectors.toList());
        return Ref.ok(vos);
    }


    /**
     * 分页查询权限列表
     */
    public CommonResult<DataGrid<MenuVo.ListQueryVo>> listQuery(MenuParam.ListQueryParam param) {

        List<SysMenu> menus = menuMapper.listQuery(param.getStart(), param.getNum(), param.getKey());
        List<MenuVo.ListQueryVo> vos = menus.stream().map(menu -> {
            MenuVo.ListQueryVo vo = new MenuVo.ListQueryVo();
            vo.setMenuId(menu.getId());
            vo.setCreateTime(TimeUtil.formater(menu.getCreateTime()));
            vo.setIcon(menu.getIcon());
            vo.setName(menu.getName());
            vo.setOrderNum(menu.getOrderNum());
            vo.setPid(menu.getPid());
            SysMenu pmenu = menuMapper.selectById(menu.getPid());
            if (pmenu != null) {
                vo.setPname(pmenu.getName());
            }
            vo.setTag(menu.getTag());
            vo.setType(menu.getType());
            vo.setUrl(menu.getUrl());
            vo.setUsable(menu.getUsable());
            return vo;
        }).collect(Collectors.toList());
        int count = menuMapper.countListQuery(param.getKey());
        DataGrid<MenuVo.ListQueryVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**
     * 启用权限
     */
    public CommonResult<Void> usable(MenuParam.UsableParam param) {
        SysMenu menu = menuMapper.selectById(param.getMenuId());
        if(menu==null){
            return Ref.error("错误的权限id");
        }
        menu.setUsable(1);
        menuMapper.updateById(menu);
        return Ref.ok();
    }

    /**
     * 禁用权限
     */
    public CommonResult<Void> disable(MenuParam.DisableParam param){
        SysMenu menu = menuMapper.selectById(param.getMenuId());
        if(menu==null){
            return Ref.error("错误的权限id");
        }
        menu.setUsable(0);
        menuMapper.updateById(menu);
        return Ref.ok();
    }


    /**
     * 删除权限
     */
    public CommonResult<Void> delete(MenuParam.DeleteParam param){
        SysMenu menu = menuMapper.selectById(param.getMenuId());
        if(menu==null){
            return Ref.error("错误的权限id");
        }
        List<SysMenu> children = menuMapper.listByPid(menu.getId());
        if(children.size()>0){
            return Ref.error("该权限有子权限,不能删除");
        }

        menuMapper.deleteById(param.getMenuId());
        return Ref.ok();
    }

    /**
     * 新增权限
     */
    public CommonResult<Void> add(MenuParam.AddParam param){

        if(param.getType()==1 && StringUtils.isEmpty(param.getIcon())){
            return Ref.error("目录权限必须设置icon");
        }
        if(param.getType()==2 && StringUtil.isEmpty(param.getUrl())){
            return Ref.error("菜单权限必须设置url");
        }

        SysMenu menu = menuMapper.getByName(param.getName());
        if(menu!=null){
            return Ref.error("该名称已经存在");
        }
        menu = menuMapper.getByTag(param.getTag());
        if(menu!=null){
            return Ref.error("该标识已经存在");
        }

        menu = new SysMenu();
        menu.setUsable(1);
        menu.setCreateTime(new Date());
        menu.setIcon(param.getIcon());
        menu.setName(param.getName());
        menu.setOrderNum(param.getOrderNum());
        menu.setPid(param.getPid());
        menu.setTag(param.getTag());
        menu.setType(param.getType());
        menu.setUrl(param.getUrl());
        menuMapper.insert(menu);
        return Ref.ok();

    }

    /**
     * 修改权限
     */
    public CommonResult<Void> modify(MenuParam.ModifyParam param){

        if(param.getType()==1 && StringUtils.isEmpty(param.getIcon())){
            return Ref.error("目录权限必须设置icon");
        }
        if(param.getType()==2 && StringUtil.isEmpty(param.getUrl())){
            return Ref.error("菜单权限必须设置url");
        }
        SysMenu menu = menuMapper.selectById(param.getMenuId());
        if(menu==null){
            return Ref.error("错误的权限编号");
        }
        SysMenu temp = menuMapper.getByNameAndNotId(param.getName(), param.getMenuId());
        if(temp!=null){
            return Ref.error("该名称已经存在");
        }
        temp = menuMapper.getByTagAndNotId(param.getTag(),param.getMenuId());
        if(temp!=null){
            return Ref.error("该标识已经存在");
        }
        menu.setUrl(param.getUrl());
        menu.setType(param.getType());
        menu.setTag(param.getTag());
        menu.setPid(param.getPid());
        menu.setOrderNum(param.getOrderNum());
        menu.setName(param.getName());
        menu.setIcon(param.getIcon());
        menuMapper.updateById(menu);
        return Ref.ok();
    }

    /**
     * 查询所有可用的权限
     */
    public CommonResult<List<MenuVo.ListUsableAllVo>> listUsableAll(){

        MemberInfo info = SystemUtil.getInfo();
        List<Integer> idList = info.getMenuList().stream().map(menu->{
            return menu.getMenuId();
        }).collect(Collectors.toList());
        List<SysMenu> sysMenus = menuMapper.listUsableByMember(idList);
        List<MenuVo.ListUsableAllVo> vos = sysMenus.stream()
                .map(menu->{
                    MenuVo.ListUsableAllVo vo = new MenuVo.ListUsableAllVo();
                    vo.setIcon(menu.getIcon());
                    vo.setMenuId(menu.getId());
                    vo.setName(menu.getName());
                    vo.setPid(menu.getPid());
                    vo.setTag(menu.getTag());
                    vo.setType(menu.getType());
                    vo.setUrl(menu.getUrl());
                    return vo;
                }).collect(Collectors.toList());
        return Ref.ok(vos);
    }

    /**
     * 测试
     */
    public CommonResult<Void> test(){

        Page<SysMenu> page = new Page<>(1, 10);
        Wrapper<SysMenu> wrapper = new EntityWrapper<SysMenu>()
                .eq("type", "1")
                .like("tag", "-")
                .orderBy("id", false);
        List<SysMenu> menus = menuMapper.selectPage(page,wrapper);
        System.out.println(JSON.toJSONString(menus));
        System.out.println(page);

        return Ref.ok();
    }
}
